(논문) STUDY
Cat/Dog_accuracy / 이미지 위에 그림 그리기
import torch
from fastai.vision.all import *
import cv2
import numpy as np
import os
os.environ['CUDA_LAUNCH_BLOCKING'] = "1"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
path=untar_data(URLs.PETS)/'images'
path
files=get_image_files(path)
def label_func(f):
if f[0].isupper():
return 'cat'
else:
return 'dog'
dls=ImageDataLoaders.from_name_func(path,files,label_func,item_tfms=Resize(512))
lrnr=cnn_learner(dls,resnet34,metrics=error_rate)
lrnr.fine_tune(1)
net1=lrnr.model[0]
net2=lrnr.model[1]
net2 = torch.nn.Sequential(
torch.nn.AdaptiveAvgPool2d(output_size=1),
torch.nn.Flatten(),
torch.nn.Linear(512,out_features=2,bias=False))
net=torch.nn.Sequential(net1,net2)
lrnr2=Learner(dls,net,metrics=accuracy)
lrnr2.fine_tune(10)
interp = ClassificationInterpretation.from_learner(lrnr2)
interp.plot_confusion_matrix()
interp.print_classification_report()
files
fig, ax = plt.subplots(5,5)
k=0
for i in range(5):
for j in range(5):
x, = first(dls.test_dl([PILImage.create(get_image_files(path)[k])]))
camimg = torch.einsum('ij,jkl -> ikl', net2[2].weight, net1(x).squeeze())
a,b = net(x).tolist()[0]
catprob, dogprob = np.exp(a)/ (np.exp(a)+np.exp(b)) , np.exp(b)/ (np.exp(a)+np.exp(b))
if catprob>dogprob:
test=camimg[0]-torch.min(camimg[0])
A1=torch.exp(-0.1*test)
X1=np.array(A1.to("cpu").detach(),dtype=np.float32)
Y1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
x1=x.squeeze().to('cpu')*Y1-torch.min(x.squeeze().to('cpu')*Y1)
(x1*0.35).squeeze().show(ax=ax[i][j])
ax[i][j].set_title("cat(%s)" % catprob.round(5))
else:
test=camimg[1]-torch.min(camimg[1])
A1=torch.exp(-0.1*test)
X1=np.array(A1.to("cpu").detach(),dtype=np.float32)
Y1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
x1=x.squeeze().to('cpu')*Y1-torch.min(x.squeeze().to('cpu')*Y1)
(x1*0.35).squeeze().show(ax=ax[i][j])
ax[i][j].set_title("dog(%s)" % dogprob.round(5))
k=k+1
fig.set_figwidth(16)
fig.set_figheight(16)
fig.tight_layout()
💡 MODE들을 CAT/DOG 폴더에 저장한다 $\to$ 사진을 불러온다(dls) $\to$ epoc=15로 학습한다(learner) $\to$ accuracy를 확인한다 $\to$ 값을 저장한다 $\to$ 총 100번 수행한 후 평균을 추출한다.
x, = first(dls.test_dl([PILImage.create(get_image_files(path)[1])]))
camimg = torch.einsum('ij,jkl -> ikl', net2[2].weight, net1(x).squeeze())
a,b = net(x).tolist()[0]
catprob, dogprob = np.exp(a)/ (np.exp(a)+np.exp(b)) , np.exp(b)/ (np.exp(a)+np.exp(b))
if catprob>dogprob:
test=camimg[0]-torch.min(camimg[0])
A1=torch.exp(-0.01*test)
X1=np.array(A1.to("cpu").detach(),dtype=np.float32)
Y1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
x1=x.squeeze().to('cpu')*Y1-torch.min(x.squeeze().to('cpu')*Y1)
else :
test=camimg[1]-torch.min(camimg[1])
A1=torch.exp(-0.01*test)
X1=np.array(A1.to("cpu").detach(),dtype=np.float32)
Y1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
x1=x.squeeze().to('cpu')*Y1-torch.min(x.squeeze().to('cpu')*Y1)
(x1*0.35).squeeze().show()
lrnr2.predict(x1)
??ImageDataLoaders.from_lists
x1=x1.reshape(1,3,512,512)
net1.to('cpu')
net2.to('cpu')
a = [] # 빈 리스트 생성
for i in range(10):
a.append(0) # append로 요소 추가
print(a)
a = []
for i in range(5) :
x, = first(dls.test_dl([PILImage.create(get_image_files(path)[i])]))
camimg = torch.einsum('ij,jkl -> ikl', net2[2].weight, net1(x).squeeze())
a,b = net(x).tolist()[0]
catprob, dogprob = np.exp(a)/ (np.exp(a)+np.exp(b)) , np.exp(b)/ (np.exp(a)+np.exp(b))
if catprob>dogprob:
test=camimg[0]-torch.min(camimg[0])
A1=torch.exp(-0.01*test)
X1=np.array(A1.to("cpu").detach(),dtype=np.float32)
Y1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
x1=x.squeeze().to('cpu')*Y1-torch.min(x.squeeze().to('cpu')*Y1)
a
else :
test=camimg[1]-torch.min(camimg[1])
A1=torch.exp(-0.01*test)
X1=np.array(A1.to("cpu").detach(),dtype=np.float32)
Y1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
x1=x.squeeze().to('cpu')*Y1-torch.min(x.squeeze().to('cpu')*Y1)
a[i]=x1
import cv2
import numpy as np
import matplotlib.pyplot as plt
from PIL import ImageDraw
from PIL import ImageFont
get_image_files(path)[0]
img = PILImage.create(get_image_files(path)[0])
img
x, = first(dls.test_dl([img]))
plt.imshow(x.squeeze().to('cpu')[0], cmap="gray")
plt.show()
img.shape
(w, h) = (img.shape[0], img.shape[1])
a=(w-512)*0.5
b=(h-512)*0.5
shape = [(a,b),(a+100,b+50)]
img1 = ImageDraw.Draw(img)
img1.rectangle(shape, fill ="black", outline ="black")
img.show()
font = ImageFont.truetype("DejaVuSans.ttf", round(h
*0.08))
ImageDraw.Draw(img).text((a,b), 'DOG', (255, 255, 255), font=font)
img.show()
img = PILImage.create(get_image_files(path)[3000])
img.show()
img.shape
x, = first(dls.test_dl([img]))
plt.imshow(x.squeeze().to('cpu')[0], cmap="gray")
plt.show()
(w, h) = (img.shape[0], img.shape[1])
a=(w-512)*0.5
b=(h-512)*0.5
shape = [(a,b),(a+100,b+50)]
#shape = [(30, 30), (130, 80)]
img1 = ImageDraw.Draw(img)
img1.rectangle(shape, fill ="white", outline ="black")
ImageDraw.Draw(img).text((a, b), 'CAT', (0,0,0), font=font)
img.show()
a=str(list(path.ls())[1]).split('/')[-1]
a.isupper()
print(str(list(path.ls())[1]).split('/')[-1][0].isupper())
print(str(list(path.ls())[0]).split('/')[-1][0].isupper())
str(list(path.ls())[0]).isupper()
get_image_files(path)[0]
dls.vocab
path=untar_data(URLs.PETS)/'images'
path
files=get_image_files(path)
def label_func(f):
if f[0].isupper():
return 'cat'
else:
return 'dog'
dls=ImageDataLoaders.from_name_func(path,files,label_func,item_tfms=Resize(512))
for i in range(7393) :
img = PILImage.create(get_image_files(path)[i])
img = img.resize([512,512], resample=None, box=None, reducing_gap=None)
(w, h) = (img.shape[0], img.shape[1])
shape = [(0, 0), (w*0.3, h*0.1)]
font = ImageFont.truetype("DejaVuSans.ttf", round(h*0.08))
name=str(list(path.ls())[i]).split('/')[-1]
if name[0].isupper() == True :
img1 = ImageDraw.Draw(img)
img1.rectangle(shape, fill ="white", outline ="black")
ImageDraw.Draw(img).text((5, 0), 'CAT', (0,0,0), font=font)
img.save("pet2/"+name, 'png')
else:
img1 = ImageDraw.Draw(img)
img1.rectangle(shape, fill ="black", outline ="black")
ImageDraw.Draw(img).text((5, 0), 'DOG', (255,255,255), font=font)
img.save("pet2/"+name, 'png')
path2=Path('pet2')
path2.ls()
files=get_image_files(path2)
dls2=ImageDataLoaders.from_name_func(path2,files,label_func,item_tfms=Resize(512))
lrnr3=cnn_learner(dls2,resnet34,metrics=error_rate)
lrnr3.fine_tune(1)
net3=lrnr3.model[0]
net4=lrnr3.model[1]
net4 = torch.nn.Sequential(
torch.nn.AdaptiveAvgPool2d(output_size=1),
torch.nn.Flatten(),
torch.nn.Linear(512,out_features=2,bias=False))
net_new=torch.nn.Sequential(net3,net4)
lrnr4=Learner(dls2,net_new,metrics=accuracy)
lrnr4.fine_tune(10)
interp = ClassificationInterpretation.from_learner(lrnr4)
interp.plot_confusion_matrix()
interp.print_classification_report()
x, = first(dls2.test_dl([PILImage.create(get_image_files(path2)[3000])]))
camimg = torch.einsum('ij,jkl -> ikl', net4[2].weight, net3(x).squeeze())
x.shape
fig, (ax1,ax2) = plt.subplots(1,2)
#
dls2.train.decode((x,))[0].squeeze().show(ax=ax1)
ax1.imshow(camimg[0].to("cpu").detach(),alpha=0.5,extent=(0,512,512,0),interpolation='bilinear',cmap='magma')
#
dls2.train.decode((x,))[0].squeeze().show(ax=ax2)
ax2.imshow(camimg[1].to("cpu").detach(),alpha=0.5,extent=(0,512,512,0),interpolation='bilinear',cmap='magma')
fig.set_figwidth(8)
fig.set_figheight(8)
fig.tight_layout()
fig, ax = plt.subplots(5,5)
k=0
for i in range(5):
for j in range(5):
x, = first(dls2.test_dl([PILImage.create(get_image_files(path2)[k])]))
camimg = torch.einsum('ij,jkl -> ikl', net4[2].weight, net3(x).squeeze())
a,b = net_new(x).tolist()[0]
catprob, dogprob = np.exp(a)/ (np.exp(a)+np.exp(b)) , np.exp(b)/ (np.exp(a)+np.exp(b))
if catprob>dogprob:
dls2.train.decode((x,))[0].squeeze().show(ax=ax[i][j])
ax[i][j].imshow(camimg[0].to("cpu").detach(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='magma')
ax[i][j].set_title("cat(%s)" % catprob.round(5))
else:
dls2.train.decode((x,))[0].squeeze().show(ax=ax[i][j])
ax[i][j].imshow(camimg[1].to("cpu").detach(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='magma')
ax[i][j].set_title("dog(%s)" % dogprob.round(5))
k=k+1
fig.set_figwidth(16)
fig.set_figheight(16)
fig.tight_layout()
x, = first(dls2.test_dl([PILImage.create(get_image_files(path2)[1])]))
camimg = torch.einsum('ij,jkl -> ikl', net4[2].weight, net3(x).squeeze())
a,b = net_new(x).tolist()[0]
catprob, dogprob = np.exp(a)/ (np.exp(a)+np.exp(b)) , np.exp(b)/ (np.exp(a)+np.exp(b))
if catprob>dogprob:
test=camimg[0]-torch.min(camimg[0])
A1=torch.exp(-0.2*test)
X1=np.array(A1.to("cpu").detach(),dtype=np.float32)
Y1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
x1=x.squeeze().to('cpu')*Y1-torch.min(x.squeeze().to('cpu')*Y1)
else :
test=camimg[1]-torch.min(camimg[1])
A1=torch.exp(-0.2*test)
X1=np.array(A1.to("cpu").detach(),dtype=np.float32)
Y1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
x1=x.squeeze().to('cpu')*Y1-torch.min(x.squeeze().to('cpu')*Y1)
(x1*0.35).squeeze().show()
x, = first(dls2.test_dl([PILImage.create(get_image_files(path2)[1])]))
camimg = torch.einsum('ij,jkl -> ikl', net4[2].weight, net3(x).squeeze())
a,b = net_new(x).tolist()[0]
catprob, dogprob = np.exp(a)/ (np.exp(a)+np.exp(b)) , np.exp(b)/ (np.exp(a)+np.exp(b))
if catprob>dogprob:
test=camimg[0]-torch.min(camimg[0])
A1=torch.exp(-0.2*test)
X1=np.array(A1.to("cpu").detach(),dtype=np.float32)
Y1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
x1=x.squeeze().to('cpu')*Y1-torch.min(x.squeeze().to('cpu')*Y1)
else :
test=camimg[1]-torch.min(camimg[1])
A1=torch.exp(-0.2*test)
X1=np.array(A1.to("cpu").detach(),dtype=np.float32)
Y1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
x1=x.squeeze().to('cpu')*Y1-torch.min(x.squeeze().to('cpu')*Y1)